<template>
  <div id="specSignHistory" class="history-container" style="display: flex;flex-direction: column;height: 100%;">
    <vxe-toolbar perfect class-name="history-top-tool-box">
      <template #buttons>
        <vxe-form :data="form" title-align="right" title-colon>
          <vxe-form-item title="条 码 号">
            <template #default="{ data }">
              <vxe-input v-model="data.reqNo" clearable @clear="findSpecSamplingHistoryList()"></vxe-input>
            </template>
          </vxe-form-item>
          <vxe-form-item title="病历号">
            <template #default="{ data }">
              <vxe-input v-model="data.patNo" clearable @clear="findSpecSamplingHistoryList()"></vxe-input>
            </template>
          </vxe-form-item>
          <vxe-form-item title="姓名">
            <template #default="{ data }">
              <vxe-input v-model="data.patNa" clearable @clear="findSpecSamplingHistoryList()"></vxe-input>
            </template>
          </vxe-form-item>
          <!-- <vxe-form-item title="标本状态">
            <template #default="{ data }">
              <vxe-select v-model="data.isSign" @change="findSpecSamplingHistoryList">
                <vxe-option v-for="(item, index) in signState" :key="index" :value="item.value"
                  :label="item.label"></vxe-option>
              </vxe-select>
            </template>
          </vxe-form-item> -->
          <!-- <vxe-form-item title="检验项目">
            <template #default="{ data }">
              <VxePulldownSelection v-enterNextInput :isChild="false" :ref="'pdown' + index"
                :url="getCompRequestUrl('reqDept')" @confirmClick="dropPullDownConfirm"
                :tableColumn="getCompShowFieldWidth('reqDept')" :aimKeys="getCompFormTargetKeyMapper('reqDept')" />
            </template>
          </vxe-form-item> -->
          <!-- <vxe-form-item title="申请科室">
            <template #default="{ data }">
              <VxePulldownSelection v-enterNextInput :isChild="false" :ref="'pdown' + index"
                :url="getCompRequestUrl('reqDept')" @confirmClick="dropPullDownConfirm"
                :tableColumn="getCompShowFieldWidth('reqDept')" :aimKeys="getCompFormTargetKeyMapper('reqDept')" />
            </template>
          </vxe-form-item> -->
          <vxe-form-item title="样本类型">
            <template #default="{ data }">
              <VxePulldownSelection v-enterNextInput :isChild="false" :ref="'pdown' + index"
                :url="getCompRequestUrl('sampleType')" @confirmClick="dropPullDownConfirm"
                :tableColumn="getCompShowFieldWidth('sampleType')" :aimKeys="getCompFormTargetKeyMapper('sampleType')" />
            </template>
          </vxe-form-item>
          <vxe-form-item title="报告单元">
            <template #default="{ data }">
              <SelectWorkGroup :url="'/api/rptGroup/findRptGroupList'" :businessType="'10'" @getSelectedId="getSelectedId"></SelectWorkGroup>
            </template>
          </vxe-form-item>
          <vxe-form-item title="入库时间">
            <template #default="{ data }">
              <vxe-input v-model="data.specSignDateS" type="datetime" clearable transfer
                @clear="findSpecSamplingHistoryList()" @change="findSpecSamplingHistoryList"
                style="width: 160px"></vxe-input>
              <span class="margin-lr-xs">-</span>
              <vxe-input v-model="data.specSignDateE" type="datetime" clearable transfer
                @clear="findSpecSamplingHistoryList()" @change="findSpecSamplingHistoryList"
                style="width: 160px"></vxe-input>
            </template>
          </vxe-form-item>

         

          <vxe-form-item title="上机时间">
            <template #default="{ data }">
              <vxe-input v-model="data.spSignDateS" type="datetime" clearable transfer
                @clear="findSpecSamplingHistoryList()" @change="findSpecSamplingHistoryList"
                style="width: 160px"></vxe-input>
              <span class="margin-lr-xs">-</span>
              <vxe-input v-model="data.spSignDateE" type="datetime" clearable transfer
                @clear="findSpecSamplingHistoryList()" @change="findSpecSamplingHistoryList"
                style="width: 160px"></vxe-input>
            </template>
          </vxe-form-item>
          <vxe-form-item>
            <template #default>
              <vxe-button class-name="margin-left-sm" icon="vxe-icon-search" type="text" class="button-blue" content="查询"
                @click="findSpecSamplingHistoryList"></vxe-button>
            </template>
          </vxe-form-item>
        </vxe-form>
      </template>
    </vxe-toolbar>

    <div class="specSignHistoryTable history-center-table-box"
      :style="{ height: 'calc(100% - 40px - 32px)', flex: '1 1 auto' }">
      <vxe-table ref="specSignHistoryTableRef" v-loading="loading" border align="left" class="mytable-scrollbar"
        :data="specSignHistoryList" :row-config="{ isCurrent: true, isHover: true }"
        :keyboard-config="{ isArrow: true, isTab: true, isEdit: true }" :height="tableHeight" @cell-click="tableCellClick"
        resizable show-overflow>
        <vxe-column field="reqNo" title="条码号" width="100px">
          <template #default="{ row }">
            <span>{{ row.reqNo ? row.reqNo : '' }}</span>
          </template>
        </vxe-column>
        <vxe-column field="patType" title="病人来源" width="60">
          <template #default="{ row }">
            {{ row.patType === '1' ? '门诊' : row.patType === '2' ? '急诊' : row.patType === '3' ? '住院' : row.patType === '4'
              ? '体检' : '未知' }}
          </template>
        </vxe-column>
        <vxe-column field="sampleTypeNa" title="样本类型" width="60"></vxe-column>
        <vxe-column field="dep" title="申请科室" width="80">
          <template #default="{ row }">
            <span>{{ row.reqDeptNa ? row.reqDeptNa : '' }}</span>
          </template>
        </vxe-column>
        <vxe-column field="remark1" title="申请号" width="80"></vxe-column>
        <!-- <vxe-column field="isSign" title="状态" width="80px" align="center" v-if="code === '70'">
          <template #default="{ row }">
            <span :class="row.isSign ? 'text-blue' : 'text-red'">
              {{ row.isSign === 1 ? '已接收' : '未接收' }}
            </span>
          </template>
        </vxe-column> -->
        <vxe-column field="patNo" title="病历号" width="100"></vxe-column>
        <vxe-column field="patNa" title="姓名" width="80"></vxe-column>
        <vxe-column field="sexNa" title="性别" width="60"></vxe-column>
        <vxe-column field="ageStr" title="年龄" width="80"></vxe-column>
        <vxe-column field="bedNo" title="床号" width="80"></vxe-column>
        <vxe-column field="reqPurpose" title="申请项目" width="180"></vxe-column>
        <vxe-column field="reqDoctorNa" title="申请人" width="80">
          <template #default="{ row }">
            <span>{{ row.reqDoctorNa ? row.reqDoctorNa : '' }}</span>
          </template>
        </vxe-column>
        <vxe-column field="reqTime" title="申请时间" width="120">
          <template #default="{ row }">
            <span>{{ row.reqTime ? row.reqTime : '' }}</span>
          </template>
        </vxe-column>
        <vxe-column field="signerNa" title="签收人" width="100" v-if="code === '70'">
          <template #default="{ row }">
            <span>{{ row.signerNa ? row.signerNa : '' }}</span>
          </template>
        </vxe-column>
        <vxe-column field="signTime" title="签收时间" width="120" v-if="code === '70'">
          <template #default="{ row }">
            <span>{{ row.signTime ? row.signTime : '' }}</span>
          </template>
        </vxe-column>
        <vxe-column field="samplingDoctorNa" title="采集人" width="100" v-if="code === '40'">
          <template #default="{ row }">
            <span>{{ row.samplingDoctorNa ? row.samplingDoctorNa : '' }}</span>
          </template>
        </vxe-column>
        <vxe-column field="samplingTime" title="采集时间" min-width="120" v-if="code === '40'">
          <template #default="{ row }">
            <span>{{ row.samplingTime ? row.samplingTime : '' }}</span>
          </template>
        </vxe-column>
        <vxe-column field="samplingDoctorNa" title="送出人" width="100" v-if="code === '50'">
          <template #default="{ row }">
            <span>{{ row.samplingDoctorNa ? row.samplingDoctorNa : '' }}</span>
          </template>
        </vxe-column>
        <vxe-column field="samplingTime" title="送出时间" min-width="120" v-if="code === '50'">
          <template #default="{ row }">
            <span>{{ row.samplingTime ? row.samplingTime : '' }}</span>
          </template>
        </vxe-column>
        <vxe-column field="samplingDoctorNa" title="送达人" width="80" v-if="code === '60'">
          <template #default="{ row }">
            <span>{{ row.samplingDoctorNa ? row.samplingDoctorNa : '' }}</span>
          </template>
        </vxe-column>
        <vxe-column field="samplingTime" title="送达时间" min-width="120" v-if="code === '60'">
          <template #default="{ row }">
            <span>{{ row.samplingTime ? row.samplingTime : '' }}</span>
          </template>
        </vxe-column>
      </vxe-table>
    </div>
  </div>
</template>
<script lang="ts" setup>
// 引入组件区 ==========
import { onMounted, ref, reactive, getCurrentInstance } from 'vue';
import { IsEmpty, formatDateTime } from '@/utils/common';
import { VxeTableEvents, VxeTableInstance } from 'vxe-table';
import SelectWorkGroup from '../../lab_bus/components/SelectWorkGroup.vue';
import VxePulldownSelection from '@/components/VxePulldownSelection.vue';
import {log} from "console";
import {isEmpty} from "lodash";
// 方法定义区 =============
onMounted(() => {
  code.value = "160";
  typeStatus.value = '0';
});
defineExpose({ initPage,getStatus });

// 全局变量定义区 ==========
let pageIndex = 1;
let totalCount = 0;
let loading = ref(false);
let specSignHistoryList = reactive([]); //列表
//let currentSpecSignHistory = {} as any; //当前选中行
let currentRowIndex = 0;
let tableHeight = ref(window.innerHeight - 190);
const specSignHistoryTableRef = ref<VxeTableInstance<RowVO>>();
let { proxy }: any = getCurrentInstance();
let signState = [
  { value: null, label: '全部' },
  {
    value: true,
    label: '已接收'
  },
  {
    value: false,
    label: '未接收'
  }
];
let form = reactive({
  patId: '', //病历号
  reqNo: '',
  patNo:'',
  patNa: '',
  specSignDateS: formatDateTime(new Date().setTime(new Date().getTime() - 3600 * 1000 * 24 * 7), 'yyyy-MM-dd 00:00:00'),
  specSignDateE: formatDateTime(new Date(), 'yyyy-MM-dd 23:59:59'),
  spSignDateS: formatDateTime(new Date().setTime(new Date().getTime() - 3600 * 1000 * 24 * 7), 'yyyy-MM-dd 00:00:00'),
  spSignDateE: formatDateTime(new Date(), 'yyyy-MM-dd 23:59:59'),
  rptGroup: '',
  rptGroupName: ''
});
let code = ref('160');
let typeStatus = ref('0');

let processAttributeList = reactive([]); //流程节点属性
let isBbth = ref(false);
function initPage(refData: any) {
  if (!IsEmpty(processAttributeList)) {
    processAttributeList.forEach((item) => {
      if (item.isRollback) {
        isBbth.value = true;
      }
    });
  }
}

function getStatus(val){
  if (isEmpty(form.rptGroup)){
    return;
  }
  if (val == '0') {
    typeStatus.value = '0';
  } else{
    typeStatus.value = '1';
  }
  findSpecSamplingHistoryList();
}


const selectAllChangeEvent: VxeTableEvents.CheckboxAll<RowVO> = ({ checked }) => {
  const $table = specSignHistoryTableRef.value
  if ($table) {
    const records = $table.getCheckboxRecords()
    proxy.$emit('clickCell', records,form.rptGroup);
    console.log(checked ? '所有勾选事件' : '所有取消事件', records)
  }
}

// 下拉报告单元获取报告单元id
function getSelectedId(selectedRadioId, selectedName) {
  form.rptGroup = selectedRadioId.value;
  form.rptGroupName = selectedName.value;
  findSpecSamplingHistoryList();
}

//获取列表
function findSpecSamplingHistoryList() {
  specSignHistoryList.length = 0;
  loading.value = true;
  const url = '/lab/reqMain/findLabReqMainPageListBySample';
  const data = {
    typeStatus: typeStatus.value,
    state: code.value,
    pageIndex: page.currentPage,
    pageSize: page.pageSize,
    patId: !IsEmpty(form.patId) ? form.patId : null,
    reqNo: !IsEmpty(form.reqNo) ? form.reqNo : null,
    patNa: !IsEmpty(form.patNa) ? form.patNa : null,
    patNo: !IsEmpty(form.patNo) ? form.patNo : null,
    rptGroup: !IsEmpty(form.rptGroup) ? form.rptGroup : null,
    samplingTimeS: !IsEmpty(form.specSignDateS) ? form.specSignDateS : null,
    samplingTimeE: !IsEmpty(form.specSignDateE) ? form.specSignDateE : null,
    sampledaBegin: !IsEmpty(form.spSignDateS) ? form.spSignDateS : null,
    sampledaEnd: !IsEmpty(form.spSignDateE) ? form.spSignDateE : null,
    
  };

  proxy.$http
    .get(url, { params: data })
    .then((res: any) => {
      specSignHistoryList.length = 0;
      specSignHistoryList.push(...res.data);
      if (specSignHistoryList.length>0){
        tableCellClick({ row: specSignHistoryList[0]});
      }
      page.totalResult = res.data.total;
      loading.value = false;
    })
    .catch((err: any) => {
      loading.value = false;
      proxy.$alert(err, '提示');
    });
}
let currentSpecSignHistory = reactive({
  id: '',
  reqNo: '',
  hisReqId: '',
  patId: '',
  patNa: '',
  sex: '',
  isSign: '',
  feeName: ''
}); //当前采集记录详情

let currentRow = reactive({});


// 表格选中当前行
function tableCellClick({ row }: any) {
  currentRow = row;
  (proxy.$refs.specSignHistoryTableRef as any).setCurrentRow(row);
  proxy.$emit('clickCell', row,form.rptGroup);
}

/**组件请求路径 */
function getCompRequestUrl(item) {
  /**审核医师 */
  if (item.fieldCode === 'chkDoctor') {
    return `/api/user/getUserInfoList`;
  }
  /**检验医师 */
  if (item.fieldCode === 'testDoctor') {
    return `/api/user/getUserInfoList`;
  }
  /**科室名称 */
  if (item.fieldCode === 'reqDept') {
    return `/api/sys/findDepartmentListbyCurrentUserOrg`;
  }
  /**样本类型 */
  if (item.fieldCode === 'sampleType') {
    return `/api/lab/labSampleType/findSampleTypeList`;
  }
  /**申请医师 */
  if (item.fieldCode === 'reqDoctor') {
    return `/api/sys/hisEmployee/finAllHisEmployeeList`;
  }
  /**采样医师 */
  if (item.fieldCode === 'samplingDoctor') {
    return `/api/sys/hisEmployee/finAllHisEmployeeList`;
  }
  /**签收医师 */
  if (item.fieldCode === 'signerDoctor') {
    return `/api/user/getUserInfoList`;
  }
  /**打印医师 */
  if (item.fieldCode === 'printDoctor') {
    return `/api/user/getUserInfoList`;
  }
  /**初审医师 */
  if (item.fieldCode === 'firstChkDoctor') {
    return `/api/user/getUserInfoList`;
  }
  /**复核医师 */
  if (item.fieldCode === 'reChkDoctor') {
    return `/api/user/getUserInfoList`;
  }
  /**采样科室 */
  if (item.fieldCode === 'samplingDept') {
    return `/api/sys/findDepartmentListbyCurrentUserOrg`;
  }
  /**采样科室 */
  if (item.fieldCode === 'samplingDept') {
    return `/lab/rptResult/findRptResultItemAll`;
  }
}
/**组件展示字段，占比宽度 */
function getCompShowFieldWidth(item) {
  /**审核医师 */
  if (item.fieldCode === 'chkDoctor') {
    return [
      { field: 'username', title: '编码', width: '40%' },
      { field: 'realname', title: '名称', width: '60%' }
    ];
  }
  /**检验医师 */
  if (item.fieldCode === 'testDoctor') {
    return [
      { field: 'username', title: '编码', width: '40%' },
      { field: 'realname', title: '名称', width: '60%' }
    ];
  }
  /**科室名称 */
  if (item.fieldCode === 'reqDept') {
    return [
      { field: 'code', title: '编码', width: '40%' },
      { field: 'name', title: '名称', width: '60%' }
    ];
  }
  /**样本类型 */
  if (item.fieldCode === 'sampleType') {
    return [
      { field: 'sampleTypeCode', title: '编码', width: '40%' },
      { field: 'sampleTypeName', title: '名称', width: '60%' }
    ];
  }
  /**申请医师 */
  if (item.fieldCode === 'reqDoctor') {
    return [
      { field: 'code', title: '编码', width: '40%' },
      { field: 'name', title: '名称', width: '60%' }
    ];
  }
  /**采样医师 */
  if (item.fieldCode === 'samplingDoctor') {
    return [
      { field: 'code', title: '编码', width: '40%' },
      { field: 'name', title: '名称', width: '60%' }
    ];
  }
  /**签收医师 */
  if (item.fieldCode === 'signerDoctor') {
    return [
      { field: 'username', title: '编码', width: '40%' },
      { field: 'realname', title: '名称', width: '60%' }
    ];
  }
  /**打印医师 */
  if (item.fieldCode === 'printDoctor') {
    return [
      { field: 'username', title: '编码', width: '40%' },
      { field: 'realname', title: '名称', width: '60%' }
    ];
  }
  /**初审医师 */
  if (item.fieldCode === 'firstChkDoctor') {
    return [
      { field: 'username', title: '编码', width: '40%' },
      { field: 'realname', title: '名称', width: '60%' }
    ];
  }
  /**复核医师 */
  if (item.fieldCode === 'reChkDoctor') {
    return [
      { field: 'username', title: '编码', width: '40%' },
      { field: 'realname', title: '名称', width: '60%' }
    ];
  }
  /**采样科室 */
  if (item.fieldCode === 'samplingDept') {
    return [
      { field: 'code', title: '编码', width: '40%' },
      { field: 'name', title: '名称', width: '60%' }
    ];
  }
}
/**组件赋值字段 */
function getCompFormTargetKeyMapper(item) {
  /**审核医师 */
  if (item.fieldCode === 'chkDoctor') {
    return [{ formKey: 'chkDoctor', rowKey: 'username' }];
  }
  /**检验医师 */
  if (item.fieldCode === 'testDoctor') {
    return [{ formKey: 'testDoctor', rowKey: 'username' }];
  }
  /**科室名称 */
  if (item.fieldCode === 'reqDept') {
    return [{ formKey: 'reqDept', rowKey: 'code' }];
  }
  /**样本类型 */
  if (item.fieldCode === 'sampleType') {
    return [{ formKey: 'sampleType', rowKey: 'sampleTypeCode' }];
  }
  /**申请医师 */
  if (item.fieldCode === 'reqDoctor') {
    return [{ formKey: 'reqDoctor', rowKey: 'code' }];
  }
  /**采样医师 */
  if (item.fieldCode === 'samplingDoctor') {
    return [{ formKey: 'samplingDoctor', rowKey: 'code' }];
  }
  /**签收医师 */
  if (item.fieldCode === 'signerDoctor') {
    return [{ formKey: 'signerDoctor', rowKey: 'username' }];
  }
  /**打印医师 */
  if (item.fieldCode === 'printDoctor') {
    return [{ formKey: 'printDoctor', rowKey: 'username' }];
  }
  /**初审医师 */
  if (item.fieldCode === 'firstChkDoctor') {
    return [{ formKey: 'firstChkDoctor', rowKey: 'username' }];
  }
  /**复核医师 */
  if (item.fieldCode === 'reChkDoctor') {
    return [{ formKey: 'reChkDoctor', rowKey: 'username' }];
  }
  /**采样科室名称 */
  if (item.fieldCode === 'samplingDept') {
    return [{ formKey: 'samplingDept', rowKey: 'code' }];
  }
}
</script>
<style lang="scss" scoped>
#specSignHistory {
  height: 100%;

  // 附加按钮间距
  .button-blue,
  .button-red {
    margin: 5px 0 5px 5px;
  }

  .vxe-form {
    :deep(.vxe-form--item) {
      margin-right: 10px;
    }
  }

  .specSignHistoryTable {
    height: calc(100vh - 40px - 32px);
  }

  .returnModal {
    :deep(.vxe-modal--body) {
      height: 100%;
    }
  }
}

:deep(.vxe-textarea-height > textarea) {
  height: calc(40vh - 10px) !important;
}

.history-container {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.history-top-tool-box {
  flex: 0 0 auto;
}

.history-center-table-box {
  flex: 1 1 auto;
  overflow: auto;
}

.history-bottom-pagesize-box {
  flex: 0 0 auto;
}</style>
